"""
该文件是IMoocDB的核心功能总入口

"""
import logging
import os

from imoocdb.sql import query_parse, query_plan
from imoocdb.executor import exec_plan, Result
from imoocdb.errors import *
from imoocdb.catalog.entry import init_catalog

empty_result = Result()
from imoocdb.constant import DEFAULT_WORKING_DIRECTORY


def notice_client(level, message):
    logging.error(message)


def init_database_working_directory(path=DEFAULT_WORKING_DIRECTORY):
    if not os.path.exists(path):
        os.makedirs(path)
    os.chdir(path)


def init_database(path=DEFAULT_WORKING_DIRECTORY):
    init_database_working_directory(path)
    init_catalog()


def exec_imoocdb_query(query_string) -> Result:
    try:
        ast = query_parse(query_string)
        plan = query_plan(ast)
        result = exec_plan(plan)
        return result
    except RollbackError as e:
        notice_client('ERROR', f'Cannot execute this query due to {e}')
        logging.exception(e)
        # todo rollback operation
    except NoticeError as e:
        notice_client('ERROR', f'Cannot execute this query due to {e}')
    except Exception as e:
        logging.exception(e)
    return empty_result


# def start_simple_terminal_client():
#     while True:
#         full_query_string = ''
#         partial_string = input('> ')
#         full_query_string += partial_string
#         while ';' not in partial_string:
#             partial_string = input('> ')
#             full_query_string += partial_string
#             result = exec_imoocdb_query(full_query_string.strip(';'))
#             print(result)
#         print('full query', full_query_string)
#

def start_simple_terminal_client():
    init_database()
    while True:
        full_query_string = ''
        partial_string = input('> ')
        full_query_string += partial_string
        while ';' not in partial_string:
            partial_string = input('> ')
            full_query_string += partial_string
        print("starting query: ", full_query_string)
        result = exec_imoocdb_query(full_query_string.strip(';'))
        print(result)


# start_simple_terminal_client()
